﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.IO;

namespace FYPDemo
{
    public partial class ProfileMaintenance : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Session["page"] = "profileMaintenance";
                btnChange.Enabled = true;
                string name = Session["username"].ToString();
                bindMemberDetails(name);

                int role = Convert.ToInt32(Session["Role"]);

                if (role == 3)
                {
                    RequiredFieldValidator6.Enabled = true;
                }
            }
        }

        protected void bindMemberDetails(string name)
        {
            Guid g = Guid.NewGuid();
            string GuidString = Convert.ToBase64String(g.ToByteArray());
            GuidString = GuidString.Replace("=", "");
            GuidString = GuidString.Replace("+", "");

            DataTable dt = Member.showMemberDetails(name);

            DisplayPicture.ImageUrl = dt.Rows[0][12].ToString() + "?r=" + GuidString;
            txtEmail.Text = dt.Rows[0][5].ToString();
            txtPhone.Text = dt.Rows[0][10].ToString();
            txtAddress.Text = dt.Rows[0][6].ToString();
            txtPostCode.Text = dt.Rows[0][7].ToString();
            txtState.Text = dt.Rows[0][8].ToString();
            txtVendorCode.Text = dt.Rows[0][16].ToString();
            ViewState["oriDP"] = dt.Rows[0][12].ToString();
            ViewState["DP"] = "ori";
        }

        protected void btnChange_Click(object sender, EventArgs e)
        {
            int result = 0;
            string checkVendorCode = "";
            string email = "";
            string phone = "";
            string address = "";
            string postcode = "";
            string state = "";
            string vendorCode = "";
            string name = Session["username"].ToString();
            string DP = DisplayPicture.ImageUrl.ToString();

            email = txtEmail.Text;
            phone = txtPhone.Text;
            address = txtAddress.Text;
            postcode = txtPostCode.Text;
            state = txtState.Text;

            if (ViewState["DP"].ToString() == "ori")
            {
                DP = ViewState["oriDP"].ToString();
            }

            else if (ViewState["DP"].ToString() == "temp")
            {
                System.IO.File.Delete(Server.MapPath(DP));
                HttpPostedFile theFile = Session["PostedFile"] as HttpPostedFile;
                uploadFile(theFile, "DisplayPicture", name);
                DisplayPicture.ImageUrl = "~/DisplayPicture/" + name + ".jpg";
                DP = DisplayPicture.ImageUrl.ToString();
            }

            if (Convert.ToInt32(Session["Role"]) == 3)
            {
                vendorCode = txtVendorCode.Text;
                DataTable dt = new DataTable();

                
                    dt = ProductManagement.checkVendorCode(vendorCode);

                    if (dt.Rows.Count > 0)
                    {
                        checkVendorCode = dt.Rows[0][0].ToString();
                    }

                if (vendorCode == "")
                {

                    if (checkVendorCode == vendorCode)
                    {
                        if (name == dt.Rows[0][1].ToString())
                        {
                            MessageBox.Show(vendorCode + " already being used by other vendors.");
                        }

                        else
                        {

                        }
                    }

                    else
                    {
                        result = Member.updateMemberDetails(name, email, phone, address, postcode, state, DP, vendorCode);

                        if (result > 0)
                        {
                            if (vendorCode != "")
                            {
                                Session["VendorCode"] = vendorCode;
                            }

                            MessageBox.Show("Profile successfully changed.");
                        }

                        else
                        {
                            MessageBox.Show("There are some errors occur during the changing process. Please try again later or contact our support team.");
                        }
                    }
                }

                else
                {
                    result = Member.updateMemberDetails(name, email, phone, address, postcode, state, DP, vendorCode);

                    if (result > 0)
                    {
                        MessageBox.Show("Profile successfully changed.");
                    }

                    else
                    {
                        MessageBox.Show("There are some errors occur during the changing process. Please try again later or contact our support team.");
                    }
                }
            }

            else
            {

                result = Member.updateMemberDetails(name, email, phone, address, postcode, state, DP, vendorCode);

                if (result > 0)
                {
                    MessageBox.Show("Profile successfully changed.");
                }

                else
                {
                    MessageBox.Show("There are some errors occur during the changing process. Please try again later or contact our support team.");
                }
            }
        }

        private void uploadFile(HttpPostedFile theFile, string uploadTo, string filename)
        {
            if (theFile != null && theFile.ContentLength > 0)
            {
                if (!Directory.Exists(Server.MapPath("~/" + uploadTo)))
                    Directory.CreateDirectory(Server.MapPath("~/" + uploadTo));

                string fname = filename + Path.GetExtension(Path.GetFileName(theFile.FileName));
                theFile.SaveAs(Server.MapPath(Path.Combine("~/" + uploadTo + "/", fname)));
            }
        }

        protected void btnUpload_Click(object sender, EventArgs e)
        {
            string tempUsername = Session["username"].ToString() + "Temp";
            string uploadTo = "DisplayPicture";

            if (FileUpload1.HasFile)
            {
                if (Directory.Exists(Server.MapPath("~/" + uploadTo + "/" + tempUsername)))
                {
                    System.IO.File.Delete(Server.MapPath("~/" + uploadTo + "/" + tempUsername));
                }

                else
                {                    
                    HttpPostedFile theFile = FileUpload1.PostedFile;
                    Session["PostedFile"] = theFile;
                    uploadFile(theFile, uploadTo, tempUsername);
                    DisplayPicture.ImageUrl = "~/" + uploadTo + "/" + tempUsername + ".jpg";
                    ViewState["DP"] = "temp";
                }
            } 
        }
    }
}